package be.kuleuven.mume.paginas;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import be.kuleuven.mume.R;
import be.kuleuven.mume.framework.Vak;
import be.kuleuven.mume.hulpklassen.DataFetcher;
import be.kuleuven.mume.hulpklassen.JSONParser.VakkenParser;
import be.kuleuven.mume.listProxys.LastCellLoadingProxy;

public class VakkenTabel extends Activity{
	private static final int ADD_VAK = 0;

	private static final int ACTIVITY_ADDVAK = 0;
	
	private ArrayList<Vak> vakken;
	private ArrayAdapter<Vak> adapter;
	private DataFetcher<Vak> datafetcher;
	private LastCellLoadingProxy loadingProxy;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vakken_tabel);
		
		tryLoadVakken();
		vakken = new ArrayList<Vak>();
		adapter = new ArrayAdapter<Vak>(this, R.layout.rij_simpel, vakken);
		loadingProxy = new LastCellLoadingProxy(adapter);
		
		ListView table = (ListView)findViewById(R.id.vakken);
		table.setAdapter(loadingProxy);
		adapter.notifyDataSetChanged();
		
		VakkenParser parser = new VakkenParser();
		datafetcher = new DataFetcher<Vak>(parser, Vak.URL, table, vakken, this.getBaseContext()){
			public void DataReceived(){
				if(!datafetcher.hasMoreData()){
					loadingProxy.setIsLoading(false);
				}
				adapter.notifyDataSetChanged();
			}
		};

		table.setTextFilterEnabled(true);

		table.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent i = new Intent(VakkenTabel.this, VakTabBar.class);
				if(adapter.getCount() > position){
					i.putExtra("VAK", adapter.getItem(position));
					startActivity(i);
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, ADD_VAK, 0, R.string.add_vak);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ADD_VAK:
			Intent intent = new Intent(this, VakkenEditPagina.class);
			startActivityForResult(intent, ACTIVITY_ADDVAK);
			break;
		default:
			break;
		}
		return true;
	}
	
	@SuppressWarnings("unchecked")
	private void tryLoadVakken() {
		Object o = getLastNonConfigurationInstance();
		if(o == null)
			vakken = new ArrayList<Vak>();
		else
			vakken = (ArrayList<Vak>)o;
	}

	@Override
	public Object onRetainNonConfigurationInstance() {
		return vakken;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == ACTIVITY_ADDVAK) {
			datafetcher.refresh();
			loadingProxy.setIsLoading(true);
		}
	}
	
	
}
